﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Cxx.Common
{
    public static class JScript
    {
        /// <summary>
        /// 弹出消息
        /// </summary>
        /// <param name="txt">TextBox控件</param>
        /// <param name="message">弹出框内容</param>
        public static void Msg(this TextBox txt, string message)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($"layer.msg('{message}');");
            ScriptManager.RegisterStartupScript(txt, txt.GetType(), "script", sb.ToString(), true);
        }

        /// <summary>
        /// 弹出对话框
        /// </summary>
        /// <param name="txt">TextBox控件</param>
        /// <param name="message">弹出框内容</param>
        public static void Alert(this TextBox txt, string message)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($"layer.alert('{message}');");
            ScriptManager.RegisterStartupScript(txt, txt.GetType(), "script", sb.ToString(), true);
        }

        /// <summary>
        /// 弹出对话框
        /// </summary>
        /// <param name="page"></param>
        /// <param name="message">弹出框内容</param>
        public static void Alert(Page page, string message)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($"layer.alert('{message}');");
            ScriptManager.RegisterStartupScript(page, page.GetType(), "script", sb.ToString(), true);
        }

        public static void Alert(Page page, string message, string callback)
        {
            string js = @"var index = parent.layer.getFrameIndex(window.name);
                            layer.confirm('" + message + @"',
                            {
                                btn: ['好的']
                            },
                            function() {
                                " + callback + @"
                            }
                        );";
            ScriptManager.RegisterStartupScript(page, page.GetType(), "script", js, true);
        }

        /// <summary>
        /// 以表单形式在新窗口中打开链接
        /// </summary>
        /// <param name="page">页面对象</param>
        /// <param name="url">链接网址</param>
        public static void RedirectInNewWindow(Page page, string url)
        {
            string redirectStr = $"window.open(\"{url}\");";
            ScriptManager.RegisterClientScriptBlock(page, page.GetType(), "script", redirectStr, true);
        }
    }
}
